﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using MyLib;

namespace Billing2.cls
{
    public partial class ClsImportSubscriber
    {
        const string SheetName = "Plan B1";
        const string TitleLine = "S/No,* Agency Short Name,* Agency Full Name,* Site,* Cost Centre,* User Name,* Service Type,* Monthly / One-Time,Subscription Start Date,Subscription End Date,Active/Inactive,* Asset ID,* Hostname,Remarks";

        public class Organization
        {
            public string org;
            public string org_fullname;
            public string site;
            public string costcentre;

            public Organization(string org, string org_fullname, string site, string costcentre)
            {
                this.org = org;
                this.org_fullname = org_fullname;
                this.site = site;
                this.costcentre = costcentre;
            }

            static public int OrgCount(List<Organization> list)
            {
                int n = 0;
                foreach (Organization org in list)
                    if (org.org != "" && org.site == "" && org.costcentre == "")
                        n++;
                return n;
            }

            static public int SiteCount(List<Organization> list)
            {
                int n = 0;
                foreach (Organization org in list)
                    if (org.org != "" && org.site != "" && org.costcentre == "")
                        n++;
                return n;
            }

            static public int CCCount(List<Organization> list)
            {
                int n = 0;
                foreach (Organization org in list)
                    if (org.org != "" && org.site != "" && org.costcentre != "")
                        n++;
                return n;
            }
        }

        public class Subscriber
        {
            public string S_No;
            public string OrgID;
            public string OrgFullname;
            public string SiteID;
            public string Costcentre;
            public string User_Name;
            public string Service_Type;
            public string Monthly_OneTime;
            public string Subscription_Start_Date;
            public string Subscription_End_Date;
            public string New_Existing_Terminated;
            public string AssetID;
            //public string Hostname;
            public string Remarks;
        }

        void ValidFile(StreamReader sr, ref int start_row)
        {
            string s = MyLib.Utility.ReadLine2(sr, 10240);

            if (s != TitleLine)
                throw new Exception("Invalid title line. title line should be '" + TitleLine + "'");
            start_row = 2;

            ////string s = "";
            ////for (int i = 1; i <= 14; i++)
            ////    s += "'" + excel.ReadString(3, i) + "',";
            ////'S/No' '* Agency Short Name' '* Agency Full Name' '* Site' '* Cost Centre' '* User Name' '* Service Type' '* Monthly / One-Time' 'Subscription Start Date' 'New/Existing/Terminated' 'Asset ID' 'Hostname' 'Subscription End Date' 'Active/Inactive'

            //int title_row = 0;
            //for(int i = 1; i <= 10; i++)
            //    if (excel.ReadString(i, 1).IndexOf("S/No") >= 0)
            //    {
            //        title_row = i;
            //        break;
            //    }
            //if (title_row == 0) throw new Exception("can not find title line");

            //int n = 1;
            //if (excel.ReadString(title_row, n).IndexOf("S/No") < 0) throw new Exception(String.Format("can not find column 'S/No' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Agency Short Name") < 0) throw new Exception(String.Format("can not find column '* Agency Short Name' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Agency Full Name") < 0) throw new Exception(String.Format("can not find column '* Agency Full Name' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Site") < 0) throw new Exception(String.Format("can not find column '* Site' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Cost Centre") < 0) throw new Exception(String.Format("can not find column '* Cost Centre' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("User Name") < 0) throw new Exception(String.Format("can not find column '* User Name' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Service Type") < 0) throw new Exception(String.Format("can not find column '* Service Type' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Monthly / One-Time") < 0) throw new Exception(String.Format("can not find column '* Monthly / One-Time' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Subscription Start Date") < 0) throw new Exception(String.Format("can not find column 'Subscription Start Date' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            ////n++;
            ////n++;
            ////n++;
            //if (excel.ReadString(title_row, n).IndexOf("Subscription End Date") < 0) throw new Exception(String.Format("can not find column 'Subscription End Date' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("New/Existing/Terminated") < 0) throw new Exception(String.Format("can not find column 'New/Existing/Terminated' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Asset ID") < 0) throw new Exception(String.Format("can not find column 'Asset ID' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Hostname") < 0) throw new Exception(String.Format("can not find column 'Hostname' at {0}{1}", Index2ColumnName(n), title_row)); n++;
            //if (excel.ReadString(title_row, n).IndexOf("Remarks") < 0) throw new Exception(String.Format("can not find column 'Remarks' at {0}{1}", Index2ColumnName(n), title_row)); n++;

            //start_row = title_row + 1;
        }

        Subscriber ReadSubscriber(StreamReader sr, ref int col)
        {
            //if (IsBottom(excel, row))
            //    return null;
            //Subscriber data = new Subscriber();
            //data.S_No = excel.ReadString(row, n).Trim(); n++;
            //data.OrgID = excel.ReadString(row, n).Trim(); n++;
            //data.OrgFullname = excel.ReadString(row, n).Trim(); n++;
            //data.SiteID = excel.ReadString(row, n).Trim(); n++;
            //data.Costcentre = excel.ReadString(row, n).Trim(); n++;
            //data.User_Name = excel.ReadString(row, n).Trim(); n++;
            //data.Service_Type = excel.ReadString(row, n).Trim(); n++;
            //data.Monthly_OneTime = excel.ReadString(row, n).Trim(); n++;
            //data.Subscription_Start_Date = excel.ReadString(row, n).Trim(); n++;
            ////n++;
            ////n++;
            ////n++;
            //data.Subscription_End_Date = excel.ReadString(row, n).Trim(); n++;
            //data.New_Existing_Terminated = excel.ReadString(row, n).Trim(); n++;
            //data.AssetID = excel.ReadString(row, n).Trim(); n++;
            //data.Hostname = excel.ReadString(row, n).Trim(); n++;
            //data.Remarks = excel.ReadString(row, n).Trim(); n++;
            //return data;

            string s = MyLib.Utility.ReadLine2(sr, 10240);
            if (string.IsNullOrEmpty(s))
                return null;
            string[] ss = s.MySplit(',', '"');
            for (int i = 0; i < ss.Length; i++)
                ss[i] = ss[i].RemoveQuote();
            col = 0;

            Subscriber data = new Subscriber();
            data.S_No = ss[col].Trim(); col++;
            data.OrgID = ss[col].Trim(); col++;
            data.OrgFullname = ss[col].Trim(); col++;
            data.SiteID = ss[col].Trim(); col++;
            data.Costcentre = ss[col].Trim(); col++;
            data.User_Name = ss[col].Trim(); col++;
            data.Service_Type = ss[col].Trim(); col++;
            data.Monthly_OneTime = ss[col].Trim(); col++;
            data.Subscription_Start_Date = ss[col].Trim(); col++;
            //n++;
            //n++;
            //n++;
            data.Subscription_End_Date = ss[col].Trim(); col++;
            data.New_Existing_Terminated = ss[col].Trim(); col++;
            col++;
            data.AssetID = ss[col].Trim(); col++;
            //remove invalid char
            //string x = data.AssetID.Substring(data.AssetID.Length - 1, 1);
            //int n = (int)x[0];
            data.AssetID = EscapeChar(data.AssetID);

            //data.Hostname = ss[col].Trim(); col++;
            data.Remarks = ss[col].Trim(); col++;
            return data;
        }

        string EscapeChar(string s)
        {
            string result = "";
            for (int i = 0; i < s.Length; i++)
            {
                int n = (int)s[i];
                if (n > 31 && n <= 255)
                    result += s[i];
            }
            return result;
        }

        //bool IsBottom(MyExcel excel, int line)
        //{
        //    try
        //    {
        //        if ((excel.ReadString(line, 1).Trim() == "") && (excel.ReadString(line + 1, 1).Trim() == ""))
        //            return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception("Search bottom failed! " + ex.Message);
        //    }
        //    return false;
        //}

        string Index2ColumnName(int n)
        {
            return n.ToString();

            //string col = "";
            //byte n1 = (byte)(n / 26);
            //byte n2 = (byte)(n - n1 * 26);
            //if (n1 > 0) col = Encoding.ASCII.GetString(new byte[] { (byte)(Encoding.ASCII.GetBytes("A")[0] + n1 - 1) }) + col;
            //if (n2 > 0) col = Encoding.ASCII.GetString(new byte[] { (byte)(Encoding.ASCII.GetBytes("A")[0] + n2 - 1) }) + col;
            //return col;
        }

        void Log(string format, params object[] objects)
        {
            string s = "";
            if (objects.Length == 0)
                s = format;
            else
                s = string.Format(format, objects);
            if (Message == "")
                Message = s;
            else
                Message = Message + "\r\n" + s;
        }

        void CleanLog()
        {
            Message = "";
        }
    }
}